﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace P2P_Backup.tools.io
{
    public class LogStreamWriter : TextWriter
	    {
	        public TextBox output = null;
            private String logString = "{0} - {1}\n";
            private String timeString = "hh:mm:ss";

	        public override void WriteLine(String value)
	        {
                value = String.Format(logString,DateTime.Now.ToString(timeString),value);
                base.WriteLine(value);
	            AppendText(value.ToString());
	        }
            private void AppendText(string text)
            {
                if (output == null) return;
                if (output.InvokeRequired)
                {
                    output.BeginInvoke((MethodInvoker)delegate { output.AppendText(text); });
                }
                else
                {
                    output.AppendText(text);
                }

            }

	        public override Encoding Encoding
	        {
	            get { return System.Text.Encoding.UTF8; }
	        }
	    }
}
